package org.jeecg.modules.demo.dary.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.TreeBuilder;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.modules.demo.dary.entity.DaRyxx;
import org.jeecg.modules.demo.dary.service.IDaRyxxService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: 人员档案信息表
 * @Author: jeecg-boot
 * @Date:   2023-11-17
 * @Version: V1.0
 */
@Api(tags="人员档案信息表")
@RestController
@RequestMapping("/dary/daRyxx")
@Slf4j
public class DaRyxxController extends JeecgController<DaRyxx, IDaRyxxService> {
	@Autowired
	private IDaRyxxService daRyxxService;
	@Autowired
	private ISysBaseAPI sysBaseAPI;
	/**
	 * 分页列表查询
	 *
	 * @param daRyxx
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "人员档案信息表-分页列表查询")
	@ApiOperation(value="人员档案信息表-分页列表查询", notes="人员档案信息表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<DaRyxx>> queryPageList(DaRyxx daRyxx,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<DaRyxx> queryWrapper = QueryGenerator.initQueryWrapper(daRyxx, req.getParameterMap());
		Page<DaRyxx> page = new Page<DaRyxx>(pageNo, pageSize);
		String xm = req.getParameter("xm");
		String zt = req.getParameter("zt");
		String sfzh = req.getParameter("sfzh");
		String xb = req.getParameter("xb");
		String csrqks = req.getParameter("csrqks");
		String csrqjs = req.getParameter("csrqjs");
		String mz = req.getParameter("mz");
		String rwsjks = req.getParameter("rwsjks");
		String rwsjjs = req.getParameter("rwsjjs");
		String twsjks = req.getParameter("twsjks");
		String twsjjs = req.getParameter("twsjjs");
		String zzmm = req.getParameter("zzmm");
		String lxdh = req.getParameter("lxdh");
		String xlqk = req.getParameter("xlqk");
		String jkzt = req.getParameter("jkzt");
		String cjxz = req.getParameter("cjxz");
		String bddh = req.getParameter("bddh");
		String twzh = req.getParameter("twzh");
		String azfs = req.getParameter("azfs");
		String jl = req.getParameter("jl");
		String rwgxmc = req.getParameter("rwgxmc");
		IPage<DaRyxx> pageList = daRyxxService.getlist(page, xm, zt, sfzh, xb, csrqks, csrqjs, mz, rwsjks, rwsjjs, twsjks, twsjjs, zzmm, lxdh, xlqk, jkzt, cjxz, bddh, twzh, azfs, jl, rwgxmc);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param daRyxx
	 * @return
	 */
	@AutoLog(value = "人员档案信息表-添加")
	@ApiOperation(value="人员档案信息表-添加", notes="人员档案信息表-添加")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody DaRyxx daRyxx) {
		daRyxxService.save(daRyxx);
		LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		String sysOrgCode  = loginUser.getOrgCode();
		String exeDepartId  = sysBaseAPI.getDepartIdsByOrgCode(sysOrgCode);
		daRyxxService.daml(loginUser.getUsername(),sysOrgCode,daRyxx.getMlmb(),daRyxx.getId());
		return Result.OK("添加成功！");
	}

	 /**
	  *   人员档案目录列表
	  *
	  * @return
	  */
	 @AutoLog(value = "人员档案目录列表")
	 @ApiOperation(value="人员档案目录列表", notes="人员档案目录列表")
	 @PostMapping(value = "/rylist")
	 public String rylist(HttpServletRequest request,@RequestBody JSONObject jsonObject) {
		 LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 String sysOrgCode  = loginUser.getOrgCode();
		 String ryxm = jsonObject.get("ryxm").toString();
		 String id = "";
		 if (jsonObject.get("id") != null  ) {
			 id = jsonObject.get("id").toString();
		 }
		 String exeDepartId  = sysBaseAPI.getDepartIdsByOrgCode(sysOrgCode);
		 List<TreeBuilder.Node> nodeList = new ArrayList<>();
		 String result = new TreeBuilder().buildTree(daRyxxService.rydaml(sysOrgCode,ryxm,id));
		 return result;
	 }


	 /**
	  *   修改人员状态
	  *
	  * @return
	  */
	 @AutoLog(value = "修改人员状态")
	 @ApiOperation(value="修改人员状态", notes="修改人员状态")
	 @GetMapping(value = "/jyupzt")
	 public String jyupzt(HttpServletRequest request,HttpServletResponse response) {
		 LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 String sysOrgCode  = loginUser.getOrgCode();
		 String id =  request.getParameter("id");
		 String zt = request.getParameter("zt");
		 String result = daRyxxService.jyupzt(id,zt).toJSONString();
		 return result;
	 }

	 /**
	  *   查看人员档案目录列表
	  *
	  * @return
	  */
	 @AutoLog(value = "查看人员档案目录列表")
	 @ApiOperation(value="查看人员档案目录列表", notes="查看人员档案目录列表")
	 @PostMapping(value = "/ckrylist")
	 public String ckrylist(HttpServletRequest req,@RequestBody JSONObject jsonObject) {
		 LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 String sysOrgCode  = loginUser.getOrgCode();
		 String ryxm = jsonObject.get("xm").toString();
		 String id = "";
		 if (jsonObject.get("id") != null  ) {
			 id = jsonObject.get("id").toString();
		 }
		 String zt = jsonObject.get("zt").toString();
		 String sfzh = "";
		 if (jsonObject.get("sfzh") != null  ) {
			 sfzh = jsonObject.get("sfzh").toString();
		 }

		 String xb     = ""; if (jsonObject.get("xb")     != null  ) {xb     =   jsonObject.get("xb").toString(); }
		 String csrqks = ""; if (jsonObject.get("csrqks") != null  ) {csrqks =   jsonObject.get("csrqks").toString(); }
		 String csrqjs = ""; if (jsonObject.get("csrqjs") != null  ) {csrqjs =   jsonObject.get("csrqjs").toString(); }
		 String mz     = ""; if (jsonObject.get("mz")     != null  ) {mz     =   jsonObject.get("mz").toString(); }
		 String rwsjks = ""; if (jsonObject.get("rwsjks") != null  ) {rwsjks =   jsonObject.get("rwsjks").toString(); }
		 String rwsjjs = ""; if (jsonObject.get("rwsjjs") != null  ) {rwsjjs =   jsonObject.get("rwsjjs").toString(); }
		 String twsjks = ""; if (jsonObject.get("twsjks") != null  ) {twsjks =   jsonObject.get("twsjks").toString(); }
		 String twsjjs = ""; if (jsonObject.get("twsjjs") != null  ) {twsjjs =   jsonObject.get("twsjjs").toString(); }
		 String zzmm   = ""; if (jsonObject.get("zzmm")   != null  ) {zzmm   =   jsonObject.get("zzmm").toString(); }
		 String lxdh   = ""; if (jsonObject.get("lxdh")   != null  ) {lxdh   =   jsonObject.get("lxdh").toString(); }
		 String xlqk   = ""; if (jsonObject.get("xlqk")   != null  ) {xlqk   =   jsonObject.get("xlqk").toString(); }
		 String jkzt   = ""; if (jsonObject.get("jkzt")   != null  ) {jkzt   =   jsonObject.get("jkzt").toString(); }
		 String cjxz   = ""; if (jsonObject.get("cjxz")   != null  ) {cjxz   =   jsonObject.get("cjxz").toString(); }
		 String bddh   = ""; if (jsonObject.get("bddh")   != null  ) {bddh   =   jsonObject.get("bddh").toString(); }
		 String twzh   = ""; if (jsonObject.get("twzh")   != null  ) {twzh   =   jsonObject.get("twzh").toString(); }
		 String azfs   = ""; if (jsonObject.get("azfs")   != null  ) {azfs   =   jsonObject.get("azfs").toString(); }
		 String jl     = ""; if (jsonObject.get("jl")     != null  ) {jl     =   jsonObject.get("jl").toString(); }
		 String rwgxmc = ""; if (jsonObject.get("rwgxmc") != null  ) {rwgxmc =   jsonObject.get("rwgxmc").toString(); }
		 String exeDepartId  = sysBaseAPI.getDepartIdsByOrgCode(sysOrgCode);
		 List<TreeBuilder.Node> nodeList = new ArrayList<>();
		 String result = new TreeBuilder().buildTree(daRyxxService.ckrydaml(sysOrgCode,ryxm,id,zt,sfzh,xb,csrqks,csrqjs,
				 mz,rwsjks,rwsjjs,twsjks,twsjjs,zzmm,lxdh, xlqk, jkzt, cjxz, bddh,twzh, azfs,jl, rwgxmc ));
		 return result;
	 }

	 /**
	  *   人员档案目录列表
	  *
	  * @return
	  */
	 @AutoLog(value = "人员档案目录列表")
	 @ApiOperation(value="人员档案目录列表", notes="人员档案目录列表")
	 @PostMapping(value = "/sqrylist")
	 public String sqrylist(HttpServletRequest request,@RequestBody JSONObject jsonObject) {
		 LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 String sysOrgCode  = loginUser.getOrgCode();
		 String ryxm = jsonObject.get("ryxm").toString();
		 String exeDepartId  = sysBaseAPI.getDepartIdsByOrgCode(sysOrgCode);
		 List<TreeBuilder.Node> nodeList = new ArrayList<>();
		 String result = new TreeBuilder().buildTree(daRyxxService.rydaml(sysOrgCode,ryxm,""));
		 return result;
	 }


	 /**
	  *  人员档案目录新建
	  *
	  * @return
	  */
	 @AutoLog(value = "人员档案目录新建")
	 @ApiOperation(value="人员档案目录新建", notes="人员档案目录新建")
	 @RequestMapping(value = "/rymladd", method = {RequestMethod.PUT,RequestMethod.POST})
	 public String rymladd(HttpServletRequest request,@RequestBody JSONObject jsonObject) {
		 LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 String sysOrgCode  = loginUser.getOrgCode();
		 String ryid = jsonObject.get("ryid").toString();
		 String mlmc = jsonObject.get("mlmc").toString();
		 String pid = "";
         if (jsonObject.get("mlid").toString().equals("-1") ){
			 pid = "0";
		 }else{
			 pid = jsonObject.get("mlid").toString();
		 }


		 String result = daRyxxService.rydamladd(loginUser.getUsername(),sysOrgCode,ryid,mlmc,pid).toString();
		 return result;
	 }

	 /**
	  *  人员档案目录修改
	  *
	  * @return
	  */
	 @AutoLog(value = "人员档案目录修改")
	 @ApiOperation(value="人员档案目录修改", notes="人员档案目录修改")
	 @RequestMapping(value = "/rymledit", method = {RequestMethod.PUT,RequestMethod.POST})
	 public String rymledit(HttpServletRequest request,@RequestBody JSONObject jsonObject) {
		 LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 String sysOrgCode  = loginUser.getOrgCode();
		 String id = jsonObject.get("treeKey").toString();
		 String mlmc = jsonObject.get("mlmc").toString();


		 String result = daRyxxService.rydamledit(loginUser.getUsername(),sysOrgCode,id,mlmc).toString();
		 return result;
	 }

	 /**
	  *  人员档案目录删除
	  *
	  * @return
	  */
	 @AutoLog(value = "人员档案目录删除")
	 @ApiOperation(value="人员档案目录删除", notes="人员档案目录删除")
	 @RequestMapping(value = "/rymldel", method = {RequestMethod.PUT,RequestMethod.POST})
	 public String rymldel(HttpServletRequest request,@RequestBody JSONObject jsonObject) {
		 LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 String sysOrgCode  = loginUser.getOrgCode();
		 String id = jsonObject.get("treeKey").toString();
		 String ryid = jsonObject.get("ryid").toString();
		 String pid = jsonObject.get("mlid").toString();

		 String result = daRyxxService.rydamldel(loginUser.getUsername(),sysOrgCode,id,ryid,pid).toString();
		 return result;
	 }
	/**
	 *  编辑
	 *
	 * @param daRyxx
	 * @return
	 */
	@AutoLog(value = "人员档案信息表-编辑")
	@ApiOperation(value="人员档案信息表-编辑", notes="人员档案信息表-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody DaRyxx daRyxx) {
		daRyxxService.updateById(daRyxx);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "人员档案信息表-通过id删除")
	@ApiOperation(value="人员档案信息表-通过id删除", notes="人员档案信息表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		daRyxxService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "人员档案信息表-批量删除")
	@ApiOperation(value="人员档案信息表-批量删除", notes="人员档案信息表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.daRyxxService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "人员档案信息表-通过id查询")
	@ApiOperation(value="人员档案信息表-通过id查询", notes="人员档案信息表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<DaRyxx> queryById(@RequestParam(name="id",required=true) String id) {

		DaRyxx daRyxx = daRyxxService.getById(id);
		if(daRyxx==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(daRyxx);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param daRyxx
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, DaRyxx daRyxx) {
        return super.exportXls(request, daRyxx, DaRyxx.class, "人员档案信息表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, DaRyxx.class);
    }

}
